var tlReqFields = {
		formid:"",
		entityName:"",
		entityName_field_split:".",
		fields_o:function(dbfieldname,fieldtype,isrepeated,i18n)
			{
				this.dbfieldname = dbfieldname;
				this.fieldtype = fieldtype;
				this.isrepeated = isrepeated;
				this.i18n = i18n;
			},
		fields_o_arr:new Array(),
		setFormID:function(formid)
		{
			this.formid = formid;
		},
		setEntityName:function(entityName)
		{
			this.entityName = entityName;
		},
		addReqF:function(dbfieldname,fieldtype,isrepeated,i18n)
		{
			var tmp_o = new this.fields_o(dbfieldname,fieldtype,isrepeated,i18n);
			this.fields_o_arr.push(tmp_o);
		},
		removeReqF:function(dbfieldname)
		{
			for (var tmp_i=0;tmp_i <this.fields_o_arr.length;tmp_i++) 
	        {  
                if (this.fields_o_arr[tmp_i].dbfieldname == dbfieldname) 
                { 
                        this.fields_o_arr.splice(tmp_i,1); 
                        break; 
                }
	        } 
		},
		checkReqF:function(fields_o)
		{
			var tmp_valid = true;
			var formobj = $('#' + this.formid);
			var realfieldname = this.entityName + this.entityName_field_split + fields_o.dbfieldname;
			switch(fields_o.fieldtype)
			{
				case "text":
				case "pointer":
				case "date":
					var field_valid = false;
					$(formobj).find('input[name=' + realfieldname + ']').each(
						function()
						{
							if ($.trim($(this).val()).length > 0)
							{
								field_valid = true;
								return false;
							}
						}
					);
					if (!field_valid)
					{
						jError(fields_o.i18n);
						if (fields_o.fieldtype != "pointer")
							document.getElementsByName(realfieldname)[0].focus();
						tmp_valid = false;
					}
					break;
				case "textarea":
					var field_valid = false;
					$(formobj).find('textarea[name=' + realfieldname + ']').each(
						function()
						{
							var textarea_value = $(this).htmlarea("toHtmlString");
							if ($.trim(textarea_value).length > 0)
							{
								if (textarea_value != "<P>&nbsp;</P>" && textarea_value != "<br>")
								{
									field_valid = true;
									return false;
								}
							}
						}
					);
					if (!field_valid)
					{
						jError(fields_o.i18n);
						tmp_valid = false;
					}
					break;
				case "list":
					var field_valid = false;
					$(formobj).find('select[name=' + realfieldname + ']').each(
						function()
						{
							if ($(this).get(0).selectedIndex > -1)
							{
								field_valid = true;
								return false;
							}
						}
					);
					if (!field_valid)
					{
						jError(fields_o.i18n);
						tmp_valid = false;
					}
					break;
			}
			return tmp_valid;
		},
		checkAllReqF:function()
		{
			var tmp_valid = true;
			for (var tmp_i = 0;tmp_i < this.fields_o_arr.length;tmp_i++)
			{
				if (!this.checkReqF(this.fields_o_arr[tmp_i]))
				{
					tmp_valid = false;
					break;
				}
			}
			return tmp_valid;
		}	
};

function checkReqForTL(formid)
{
	tlReqFields.setFormID(formid);
	return tlReqFields.checkAllReqF();
}

function addReqFForTL(dbfieldname,fieldtype,isrepeated,i18n)
{
	tlReqFields.addReqF(dbfieldname,fieldtype,isrepeated,i18n);
}

function removeReqFForTL(dbfieldname)
{
	tlReqFields.removeReqF(dbfieldname);
}

function setEntityNameForTL(entityname)
{
	tlReqFields.setEntityName(entityname);
}

function template_listview_show(moducode,link)
{
	link = link + "&rd=" + Math.random();
	$('#'+moducode+'ViewDialog').find('iframe[id='+ moducode +'ViewFrame]').attr({src:link});
	$('#'+moducode+'ViewDialog').dialog('open');
}

function checkEncyReqField(formid){
	var flag = true;
	$("#"+formid).find('font[name=reqfield]').each(
		function(){
			var type = $(this).attr('type');
			if(type=='text'){
				$("#"+formid).find('input[type=text][name='+$(this).attr('id')+']').each(
					function(){
						if($.trim($(this).val()).length == 0){
							flag = false;
							jError($(this).attr('var') + " 不能为空");
							return false;
						}
					}
				);
			}else if(type=='date'){
				$("#"+formid).find('input[type=text][name='+$(this).attr('id')+']').each(
					function(){
						if($.trim($(this).val()).length == 0){
							flag = false;
							jError($(this).attr('var') + " 不能为空");
							return false;
						}
						var reg = new RegExp("\d{4}-\d{1,2}-\d{1,2}");
						if(!reg.exec($.trim($(this).val()))){
							flag = false;
							jError($(this).attr('var') + " 的日期格式必须为yyyy-mm-dd");
							return false;
						}
					}
				);
			}else if(type=='textarea'){
				$("#"+formid).find('textarea[name='+$(this).attr('id')+']').each(
						function(){
							if($.trim($(this).htmlarea()).length==0){
								flag = false;
								jError($(this).attr('var')+ " 不能为空");
								return false;
							}
						}
				);
			}
		}
	);
	return flag;
}